package com.xzh.gulimall.threadTest;

import java.util.concurrent.*;

public class ThreadTest_01 {
    public static ExecutorService execotor = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main开始");
        /*CompletableFuture.runAsync(()->{
            System.out.println("当前线程，"+Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果="+i);
        },execotor);*/
        /*CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程，" + Thread.currentThread().getId());
            int i = 10 / 2;
            //int p = i / 0;
            System.out.println("运行结果=" + i);
            return i;
        }, execotor).whenComplete((res,ex)->{
            //虽然能得到异常信息，但是没法修改返回数据
            System.out.println("异步任务完成了，res = "+res+"  异常是 +"+ex);
        }).exceptionally(throwable -> {
            //可以感知异常同时返回默认值,如果上述p没注释。就是有异常，下面调用get方法就是返回10
            return 10;
        });*/
        /*CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程，" + Thread.currentThread().getId());
            int i = 10 / 2;
            int p = i / 0;
            System.out.println("运行结果=" + i);
            return i;
        }, execotor).handle((res,e)->{
            if(res!=null)
            {
                return res*2;
            }
            if(e!=null)
            {
                return  0 ;
            }
            return -1;
        });*/
        /*
        * 线程串行化
        * 1）、thenRunAsync不能获取上一步的运行结果，无返回值
        *     thenRunAsync(()->{
                 System.out.println("任务2启动了。。。");
              },execotor);
          2）、thenAcceptAsync可以获取上一步的返回结果，无返回值
          * thenAcceptAsync((res)->{
            System.out.println("上一步返回结果为："+res);
            System.out.println("任务2启动了。。。");
           },execotor);
          3)、thenApplyAsync 可以获取上一步的返回值，也有返回结果
          *   thenApplyAsync((res)->{
                System.out.println("上一步返回结果为："+res);
                System.out.println("任务2启动了。。。");
                return "hello"+ res;
             },execotor);*/
        /*CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程，" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果=" + i);
            return i;
        }, execotor).thenApplyAsync((res)->{
            System.out.println("上一步返回结果为："+res);
            System.out.println("任务2启动了。。。");
            return "hello"+ res;
        },execotor);*/

        /*future1.runAfterBothAsync(future2,()->{
            System.out.println("任务三开始");
        },execotor);*/
        /*future1.thenAcceptBothAsync(future2,(res1,res2)->{
            System.out.println("任务三开始。。之前的结果 任务一："+res1+" 任务二："+res2);
        },execotor);*/
        /*CompletableFuture<String> future = future1.thenCombineAsync(future2, (res1, res2) -> {
            System.out.println("任务三执行");
            return res1 + ":" + res2 + ",hello";
        }, execotor);*/

        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品图片信息");
            return "hello.jpg";
        }, execotor);
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品属性信息");
            return "黑色+256G";
        }, execotor);
        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {

            try {
                Thread.sleep(3000);
                System.out.println("查询商品介绍");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "华为";
        }, execotor);
        /*CompletableFuture<Void> allOf = CompletableFuture.allOf(future1, future2, future3);
        allOf.get();//等待所有接结果完成,get方法返回null*/
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future1, future2, future3);
        anyOf.get();//只要其中一个完成,返回完成的那个线程返回的结果
        System.out.println("main结束+f" + anyOf.get());

    }
}


